package com.markerhub.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.SysDict;
import com.markerhub.entity.SysDictValue;
import com.markerhub.entity.SysRoleMenu;
import com.markerhub.entity.SysUserRole;
import com.markerhub.service.SysDictService;
import com.markerhub.service.SysDictValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.ElementType;
import java.util.Arrays;
import java.util.List;

/**
 * @author jsx on 2023/1/15
 **/
@RestController
@RequestMapping("/sys/dict")
public class SysDictController extends BaseController{

    @Autowired
    private  SysDictService sysDictService;
    @Autowired
    private SysDictValueService sysDictValueService;

    @GetMapping("/list")
    public Result getList(){
        List<SysDict> dictList = sysDictService.list();
        return Result.succ(dictList);
    }

    @PostMapping("/delete")
    @Transactional
    public Result info(@RequestBody String[] ids) {
        sysDictService.removeByCodes(ids);
        for (String id : ids) {
            sysDictValueService.remove(new QueryWrapper<SysDictValue>().eq("type_code",id));
        }
        return Result.succ("");
    }

    @GetMapping("/info/{code}")
    public Result info(@PathVariable("code") String code){
        SysDict sysDict = sysDictService.getOne((new QueryWrapper<SysDict>().eq("code",code)));
        return Result.succ(sysDict);
    }

    @PostMapping("/save")
    public Result save(@Validated @RequestBody SysDict sysDict){
        //先查询code是否存在
        SysDict oldSysDict = sysDictService.getOne((new QueryWrapper<SysDict>().eq("code",sysDict.getCode())));
        if(oldSysDict !=  null){
            //进行修改
            sysDictService.update(sysDict,new QueryWrapper<SysDict>().eq("code",sysDict.getCode()));
        }else {
            //进行增加
            sysDictService.save(sysDict);
        }
        return Result.succ("");
    }

    @GetMapping("/value/list/{typeCode}")
    public Result getValueList(@PathVariable("typeCode") String  typeCode){
        List<SysDictValue> dictList = sysDictValueService.getList(typeCode);
        return Result.succ(dictList);
    }

    @PostMapping("/value/save")
    public Result save(@RequestBody SysDictValue sysDictValue){
        sysDictValueService.save(sysDictValue);
        return Result.succ("");
    }

    @PostMapping("/value/delete")
    @Transactional
    public Result deleteValue(@RequestBody String[] ids) {
        sysDictValueService.removeByIds(Arrays.asList(ids));
        return Result.succ("");
    }

    @GetMapping("/value/info/{id}")
    public Result getValueInfo(@PathVariable("id") String id){
        SysDictValue value = sysDictValueService.getById(id);
        return Result.succ(value);
    }

    @PostMapping("/value/update")
    public Result update(@RequestBody SysDictValue sysDictValue){
        sysDictValueService.updateById(sysDictValue);
        return Result.succ("");
    }


}
